package org.community.detail.controller;

import com.alibaba.fastjson.JSONObject;
import entity.CommonResult;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.community.detail.service.detail.userInfo.UserInfoServiceMain;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.util.Map;

@RestController
@Data
@Slf4j
@RequestMapping("/userInfo")
public class UserInfoController {

    @Resource
    private UserInfoServiceMain userInfoServiceMain;


    @PostMapping("")
    public CommonResult<String> setUserInfo(@RequestBody JSONObject userInfo) throws ParseException, IOException {

        int code = this.getUserInfoServiceMain().userInfoUpdate(userInfo);
        if(code == 1){
           return new CommonResult<>(200,"Successful","");
        }

        if(code == -1){
            return new CommonResult<>(400,"Failed,username is exist","");
        }

        return new CommonResult<>(404,"Failed","");

    }


    @GetMapping("/{userId}/{choice}")
    public CommonResult<Map<String,Object>> getUserInfo(@PathVariable("userId") Integer userId,@PathVariable("choice") Integer choice) throws ParseException, IOException {
        Map<String,Object> userInfoMap = this.getUserInfoServiceMain().userInfoShow(userId,choice == 1);
        if(userInfoMap != null){
            return new CommonResult<>(200,"Successful",userInfoMap);
        }
        return new CommonResult<>(404,"Not Found",null);


    }
}
